import React, { useState, useEffect} from 'react';
import { useHistory } from "react-router-dom";
import Btn from "../../components/btn/Btn";
import UserMsg from "../outUser/userMsg/UserMsg"; 
import http from '../../model/http';
import code from "../../model/code";
import './billing.less'; 
import {ActivityIndicator, Toast} from 'antd-mobile';

function Billing() {
  const [ moreInfo, setMoreInfo ] = useState({}) //客户详细资料
  const [ hadRequest, setHadRequest] = useState(false)
  const [ entryIsHome, setEntryIsHome] = useState(0);  // 页面入口判断
  const [ outUserId, setOutUserId ] = useState({})  
  const [animatingShow,setAnimatingShow] = useState(false)
  const [authority,setAuth ] =useState()


  const getOutUser = async (outUserId)=>{
    const data = await http.post('/customer/checkCustomerBindoutUserId', {outUserId});
    console.log("获取外部联系人情况",data.data)
    // setList(data.data)
    // setIsBindOutUesr(data.isBind)
    if(data.isBind===1){
        // history.push(`/user/details?isBind=${1}&outUserId=${outUserId}&entryIsHome=${entryIsHome}&showHeader=0`, data.data[0]); //跳转到客户详情页面
        return
    }else{
        setHadRequest(true)
        history.push('/user')
    }

  }

  /*渲染完成*/
  // eslint-disable-next-line react-hooks/exhaustive-deps
  useEffect( async ()=>{
    setAnimatingShow(true)
    // 判断是否绑定
    await code.checkBind() 
    const result =  await code.getContext()


    //获取权限  http://wxwork.gzsg.cn/wxwork/manager/userInfoInit
    const data1 = await http.post('/manager/userInfoInit', {});
    console.log("data1",data1)
    if(data1.code == 0){
      setAuth(data1.authority)
    }




    // console.log(history.location.state) 
    // if(history.location.state != undefined && history.location.state.customerCrmId !=''){
    //   getCustomerDetails(history.location.state.customerCrmId)
    //   return false
    // }
    if(sessionStorage.getItem('getHomeCustomerCrmId')!='' && sessionStorage.getItem('getHomeCustomerCrmId')!=undefined && sessionStorage.getItem('getHomeCustomerCrmId')!=null ){
      getCustomerDetails(sessionStorage.getItem('getHomeCustomerCrmId'))
      return false
    }


    if(result=="single_chat_tools"||result=="contact_profile"){
      // 获取外部联系人
      const outUserId = await code.getOutUserId()
      setOutUserId(outUserId)
      
      console.log('outUserId:',outUserId)
      getOutUser(outUserId)
     
      const data = await http.post('/customer/checkCustomerBindoutUserId', {outUserId});
       
      console.log('data：',data)
      if(data.isBind===1){ 
        getCustomerDetails(data.data[0].customerCrmId)  //获取客户详细资料
      } 
    }else{
      setHadRequest(true)
      setEntryIsHome(1)
    }
    
  },[])
  
  //获取详细资料
  const getCustomerDetails = (customerCrmId)=>{  
    http.post('/customer/customerDetail',{
      customerCrmId:customerCrmId,
    }).then(res=> { 
      setMoreInfo(res.baseInfo[0])  //客户详细资料
      sessionStorage.setItem('userInfoDetail',JSON.stringify(res.baseInfo[0]))
      setAnimatingShow(false)
    })
  }
  const history = useHistory();
  const billingGo = path =>{
    history.push(path,moreInfo); 
  } 

  const billingGo2 = () => {
    Toast.fail('即将上线，敬请期待')
  }
  return ( 
    <div className='v-billing'>
      <div className="body-background-div"></div>
      <UserMsg msg={moreInfo} />
      <div className='g-abs g-b0 g-l0 g-w100'>
        { (authority == 1 ||authority == 2||authority == 9 )? <div>

          <Btn className="v-billing-box-btn" type='warning' onClick={()=>{billingGo('/billing/project')}}>项目开单</Btn>
          <Btn  type='primary'  
            onClick={()=>{billingGo('/billing/cardCharging')}}  
            className="appointment-box v-yuyue-box-btn">
            储蓄充卡 
          </Btn> 


        </div> :<div></div>}

       
      </div> 
      <ActivityIndicator
        toast
        text="Loading..."
        animating={animatingShow}
      />
    </div>
  )
}

export default Billing;
 